<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/22 0022
 * Time: 10:43
 */
echo "process-start-time:".time().PHP_EOL;
$urls = [
    'http://baidu.com',
    'http://sina.com.cn',
    'http://qq.com',
    'http://baidu.com?search=singwa',
    'http://baidu.com?search=singwa2',
    'http://baidu.com?search=imooc',
];

//传统方式
//foreach ($urls as $url){
//    $content[] = file_get_contents($url);
//}

for($i=0; $i<count($urls); $i++){
    //子进程
    $process = new swoole_process(function($worker) use($i, $urls){
        //curl
        $content = curlData($urls[$i]);
//        echo $content . PHP_EOL;
        $worker->write($content.PHP_EOL);
    }, true, true);  //输出到管道
    $pid = $process->start();
    $workers[$pid] = $process;
}

foreach($workers as $process){
    echo $process->read();
}


/**
 * 模拟请求url的内容 耗时1s
 * @param $url
 * @return string
 */
function curlData($url){
    // curl  file_get_contents
    sleep(1);
    return $url . 'success' . PHP_EOL;
}

echo "process-end-time:".time().PHP_EOL;









